export {};

//  js会根据判断条件 做类型保护
interface IPerson1 {
  kind: "1";
  handsome: string;
  name: string;
}

interface IPerson2 {
  kind: "2";
  high: string;
  name: number;
}

function isPerson(val: IPerson2 | IPerson1) {
  if ("handsome" in val) {
    return val; // IPerson1
  } else {
    return val; // IPerson2
  }
}

// 通过一个标识属性区分类型  ： 可辨识类型

function isPersonByKind(val: IPerson2 | IPerson1) {
  if (val.kind === "1") {
    return val; // IPerson1
  } else {
    return val; // IPerson2
  }
}

// 自定义类型保护
function isOne(val: IPerson2 | IPerson1): val is IPerson1 {
  return val.kind === "1";
}

function isPersonByKind2(val: IPerson2 | IPerson1) {
  if (isOne(val)) {
    // return val; // val is IPerson1时 IPerson1
    return val; // IPerson1 | IPerson2
  } else {
    // return val; // val is IPerson1时 IPerson2
    return val; // IPerson1 | IPerson2
  }
}
